<script>
function refreshdata()
{
	<?php
	echo $js->request(array('controller'=>'plugins','action'=>'showpermgroups'),array('method'=>'post','dataExpression'=>true,'data'=>'{world:$("#worldlist").val()}','type'=>'json','update'=>'#plugins_showpermgroups'));
	echo $js->request(array('controller'=>'plugins','action'=>'showpermusers'),array('method'=>'post','dataExpression'=>true,'data'=>'{world:$("#worldlist").val()}','type'=>'json','update'=>'#plugins_showpermusers'));
	?>
}

$(document).ready(function() {
	$("#accordion").accordion({ autoHeight: false ,collapsible: true, clearStyle: true});
	});

function editpermgroup(data)
{
	if ($('#worldlist').val()!="")
	{
		var strTitle;
		if (data=="")
		{
			data = new Array;
			data[0]="";
			data[1]="";
			data[2]="";
			data[3]="";
			data[4]="";
			data[5]= new Array('');
			data[6]= new Array('');
			strTitle = "Add group";
		}
		else
		strTitle = "Edit group";
		
		if (data[5]==null)
			strInheritance = "";
		else
		strInheritance = data[5].join('\n');
		
		if (data[6]==null)
			strPermissions = "";
		else
		strPermissions = data[6].join('\n');
		
		$('#groupWorld').val($('#worldlist').val());
		$('#groupName').val(data[0]);
		$('#groupDefault').attr('checked',data[1]);
		$('#groupPrefix').val(data[2]);
		$('#groupSuffix').val(data[3]);
		$('#groupBuild').attr('checked',data[4]);
		$('#groupInheritance').val(strInheritance);
		$('#groupPermissions').val(strPermissions);
		$.blockUI({
			theme: true,
			css:{ 
				padding:        0, 
				margin:         0, 
				width:          '300px'
				},
			title:    strTitle,
			fadeIn:  300,
			message: $('#addgroup')});
	}
	else
	alert("<?php echo __('You must choose a world!',true); ?>");
}

function editpermuser(data)
{
	if ($('#worldlist').val()!="")
	{
		var strTitle;
		if (data=="")
		{
			data = new Array;
			data[0]="";
			data[1]="";
			data[2]="";
			data[3]="";
			data[4]= new Array('');
			strTitle = "Add user";
		}
		else
		strTitle = "Edit user";
		
		if (data[4]==null)
			strPermissions = "";
		else
			strPermissions = data[4].join('\n');
		
		$('#userWorld').val($('#worldlist').val());
		$('#userName').val(data[0]);
		$('#userPrefix').val(data[1]);
		$('#userSuffix').val(data[2]);
		$('#userGroup').val(data[3]);
		$('#userPermissions').val(strPermissions);
		$.blockUI({
			theme: true,
			css:{ 
				padding:        0, 
				margin:         0, 
				width:          '300px'
				},
			title:    strTitle,
			fadeIn:  300,
			message: $('#adduser')});
	}
	else
	alert("<?php echo __('You must choose a world!',true); ?>");
}

</script>
<?php
echo $form->label('world','Select World : ');
echo $form->select('world',$worlds,NULL,array('div'=>false,'id'=>'worldlist'));

$js->get('#worldlist');
echo $js->event('change','refreshdata()');
?>
<hr/>
<div id="accordion">
<h3><a href="#"><h3>Groups</h3></a></h3>
<div>
<table class="box-table-a">
<thead>
<tr>
<th scope='col'>Name of group</th>
<th scope='col'>Default</th>
<th scope='col'>Prefix</th>
<th scope='col'>Suffix</th>
<th scope='col'>Build</th>
<th scope='col'>Inheritance</th>
<th scope='col'>Permissions</th>
<th scope='col'></th>
<th scope='col'></th>
</tr>
</thead>
<tbody id="plugins_showpermgroups">
<?php echo $this->element('plugins_showpermgroups'); ?>
</tbody>
<tfoot>
<tr><td colspan="9" align="center"><a class="ui-icon ui-icon-plus" href="#" onclick="editpermgroup('');"></a></td></tr>
</tfoot>
</table>	
</div>
<h3><a href="#"><h3>Users</h3></a></h3>
<div>
<table class="box-table-a">
<thead>
<tr>
<th scope='col'>Name of user</th>
<th scope='col'>Prefix</th>
<th scope='col'>Suffix</th>
<th scope='col'>Group</th>
<th scope='col'>Permissions</th>
<th scope='col'></th>
<th scope='col'></th>
</tr>
</thead>
<tbody id="plugins_showpermusers">
<?php echo $this->element('plugins_showpermusers'); ?>
</tbody>
<tfoot>
<tr><td colspan="8" align="center"><a class="ui-icon ui-icon-plus" href="#" onclick="editpermuser('');"></a></td></tr>
</tfoot>
</table>	
</div>
</div>

<div id="addgroup" class="dialog" style="display:none">
<?php
echo $form->create('group');
echo $form->input('world',array('type'=>'hidden'));
echo $form->input('name',array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->label('default',__("Default",true));
echo $form->checkbox('default',array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->label('build',__("Build",true));
echo $form->checkbox('build',array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->input('prefix',array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->input('suffix',array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->label('inheritance',__("Inheritance",true));
echo $form->textarea('inheritance',array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->label('permissions',__("Permissions - Enter one permission per line",true));
echo $form->textarea('permissions',array('div'=>false,'style'=>'height:150px;'));
echo "<br/>";
echo "<br/>";
?>
<center>
<?php
echo $js->submit(
'save',
array('div'=>false,'url'=>array('controller'=>'plugins','action'=>'editpermgroup'),'update'=>'#plugins_showpermgroups','complete' => '$.unblockUI();'));
?>
<input type="reset" value="cancel" onclick="$.unblockUI();">
</center>
<?php echo $form->end(); ?>
</div>

<div id="adduser" class="dialog" style="display:none">
<?php
echo $form->create('user');
echo $form->input('world',array('type'=>'hidden'));
echo $form->input('name',array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->input('prefix',array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->input('suffix',array('div'=>false));
echo "<br/>";
echo "<br/>";
//echo $form->input('group',array('div'=>false));
echo $form->label('group',__("Group",true));
echo $form->select('group',NULL,NULL,array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->label('subgroups',__("Subgroups",true));
echo $form->textarea('subgroups',array('div'=>false));
echo "<br/>";
echo "<br/>";
echo $form->label('permissions',__("Permissions - Enter one permission per line",true));
echo $form->textarea('permissions',array('div'=>false,'style'=>'height:150px;'));
echo "<br/>";
echo "<br/>";
?>
<center>
<?php
echo $js->submit(
'save',
array('div'=>false,'url'=>array('controller'=>'plugins','action'=>'editpermuser'),'update'=>'#plugins_showpermusers','complete' => '$.unblockUI();'));
?>
<input type="reset" value="cancel" onclick="$.unblockUI();">
</center>
</div>
<?php
echo $this->Js->writeBuffer();
?>